package com.mercenary.wein.screenControl.ui.batch;
import com.mercenary.wein.screenControl.command.CommandTool;
import com.mercenary.wein.screenControl.ui.util.CommonTools;
import com.mercenary.wein.screenControl.ui.util.Constants;

import javax.swing.JDialog;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ComboBoxEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;


public class AddSearchFriend  extends JDialog  {
	// Log log = LogFactory.getLog(AddSearchFriend.class);
	JLabel label;
	GroupCheckBoxPanel J1 = new GroupCheckBoxPanel();

	JPanel J2= new JPanel();
	JPanel J3= new JPanel();
	//J2的面板
	JPanel J9= new JPanel();
	JScrollPane J10= new JScrollPane();

	static JFrame jf=new JFrame();
	public  AddSearchFriend (){
		super(jf,"添加搜索好友");
		setBounds(200, 100, 700, 550);
		setLayout(new BorderLayout());
		setVisible(true);
		add(J1,BorderLayout.WEST);
		add(J2,BorderLayout.CENTER);
		add(J3,BorderLayout.EAST);
		J1.setPreferredSize(new Dimension(180,550));//设置大小
		J1.setBackground(Color.WHITE);
		J2.setPreferredSize(new Dimension(270,550));
		J2.setBackground(Color.WHITE);
		J2.add(J9);
		J2.add(J10);
		J9.setPreferredSize(new Dimension(250,50));
		J10.setPreferredSize(new Dimension(250,500));
		J10.setBackground(Color.pink);
		J3.setPreferredSize(new Dimension(250,550));//设置大小
		J3.setBackground(Color.WHITE);
		Border titleBorder2=BorderFactory.createTitledBorder("从文件导入号码");
		J2.setBorder(titleBorder2);

		final JLabel j2 = new JLabel("已准备号码数:0");
		//初始化表格
		Vector<String> columnNames = new Vector<String>();
		columnNames.add("序号");
		columnNames.add("微信号/手机号/qq号");
		columnNames.add("发送验证申请");
		Vector<List<String>> columnData = new Vector<List<String>>();
		DefaultTableModel dm = new DefaultTableModel(columnData, columnNames);
		final JTable friends = new JTable(dm);
		final List<String> phoneNums = new ArrayList<String>();
		DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
		J10.setViewportView(friends);
		tcr.setHorizontalAlignment(SwingConstants.CENTER);// 这句和上句作用一样
		friends.setDefaultRenderer(Object.class, tcr);
		friends.setPreferredScrollableViewportSize(new Dimension(100, 20));
		J10.validate();
		JButton B4 = new JButton("选择文件");
		B4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser(Constants.RepertoryPath);// 以c:\\winnt为打开文件为对话框的默认路径
				// 利用addChoosableFileFilter()方法加入欲过滤的文件类型,使用addChoosableFileFilter()可以加入多种文件类型,
				// 若你只需要过滤出一种文件类型,可使用setFileFilter()方法.
				fileChooser.addChoosableFileFilter(new JavaFileFilter("class"));
				fileChooser.addChoosableFileFilter(new JavaFileFilter("java"));
				int result = fileChooser.showOpenDialog(jf);
				// /如果选择确定按钮的话，那么则可以打开现有的文件
				if (result == JFileChooser.APPROVE_OPTION) {
					File file = fileChooser.getSelectedFile();
					Vector<String[]> address = readFile(file);
					phoneNums.removeAll(phoneNums);

					DefaultTableModel dm = ((DefaultTableModel) friends.getModel());
					while (dm.getRowCount() > 0) {
						dm.removeRow(0);
					}
					for (String[] s : address) {
						dm.addRow(s);
						phoneNums.add(s[1]+":"+s[2]);
					}
					dm.fireTableDataChanged();
					j2.setText("已准备号码数:" + address.size());
					j2.repaint();
					//label.setText("你选择了:" + file.getName() + "文件");
				}
				// /否则如果单击取消的话，那么就会在标签中显示"你没有选取文件"
				else if (result == fileChooser.CANCEL_OPTION) {
					label.setText("你没有选取文件");
				}
			}
		});
		JButton B5 = new JButton("清空数据");
		B5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DefaultTableModel dm = ((DefaultTableModel) friends.getModel());
				while (dm.getRowCount() > 0) {
					dm.removeRow(0);
				}
				dm.fireTableDataChanged();
				phoneNums.removeAll(phoneNums);
				j2.setText("已准备号码数:" + 0);
				j2.repaint();
			}
		});
		J9.add(B4);
		J9.add(B5);
		J9.validate();


		//J3组件
		FlowLayout layout = new FlowLayout(FlowLayout.LEADING ) ;
		J3.setLayout(layout);
		JPanel jp0=new JPanel(new GridLayout(2,1));
		JPanel jp1=new JPanel(new FlowLayout(FlowLayout.LEADING));
		JPanel jp2=new JPanel(new FlowLayout(FlowLayout.LEADING ));
		final ExeModeButtonGroup jp3=new ExeModeButtonGroup(this);
		JPanel jp4=new JPanel(new FlowLayout(FlowLayout.CENTER));
		final JPanel jptime=new JPanel();
		jp0.setPreferredSize(new Dimension(300,60));//设置大小
		jp1.setPreferredSize(new Dimension(300,30));//设置大小
		jp2.setPreferredSize(new Dimension(300,30));//设置大小
		jp3.setPreferredSize(new Dimension(300,120));//设置大小
		jp4.setPreferredSize(new Dimension(300,40));//设置大小
		SelectedNumLabel j1 = new SelectedNumLabel();
		J1.registerObserver(j1);
		JLabel L1=new JLabel("每部手机添加次数:");
		JLabel L4=new JLabel("~");
		JLabel L5=new JLabel("秒");
		final JTextField  num =new JTextField();
		num.setPreferredSize( new Dimension(35,25) );
		final JTextField  start =new JTextField();
		start.setPreferredSize( new Dimension(35,25) );
		final JTextField  end =new JTextField();
		end.setPreferredSize( new Dimension(35,25) );
		JCheckBox  j3=new  JCheckBox("间隔");

		jp0.add(j1) ;
		jp0.add(j2);
		jp1.add(L1);
		jp1.add(num);
		jp2.add(j3);
		jp2.add(start);
		jp2.add(L4);
		jp2.add(end );
        jp2.add(L5);

		JButton submit=new JButton("提交任务");
		jp4.add(submit);
		J3.add(jp0);
		J3.add(jp1);
		J3.add(jp2);
		J3.add(jp3);
		J3.add(jp4);

        submit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CommandTool.addFriendsFromSearch(J1.getSelectedDevices(),phoneNums,jp3.getExeModeBean());
            }
        });

		J3.validate();
	}
	public  static void main(String[] arge){
		new AddSearchFriend ();


	}

	public Vector<String[]> readFile(File addressBook) {
		try {
			Vector<String[]> address = new Vector<String[]>();
			BufferedReader reader = new BufferedReader(new FileReader(addressBook));
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				//   log.info("line " + line + ": " + tempString);
                String[] tmps= new String[3];
                tmps[0]=line+"";
                tmps[1]=tempString.split(",")[0];
                if(tempString.split(",").length==1){
                    tmps[2]="";
                }else tmps[2]=tempString.split(",")[1];
				address.add(tmps);
				line++;
			}
			reader.close();
			return address;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			// log.info("importAddressBook --- file not exists");
		} catch (IOException e) {
			e.printStackTrace();
			//  log.info("importAddressBook --- file io error");
		}
		return null;
	}
}

		
	
			
			